package com.alpha.eceasy.mall.controller;

import com.alpha.eceasy.mall.api.dto.MallCommoditySpecificationDTO;
import com.alpha.eceasy.mall.api.entity.MallCommoditySpecification;
import com.alpha.eceasy.core.R;
import com.alpha.eceasy.mall.api.vo.MallCommoditySpecificationVO;
import com.alpha.eceasy.mall.service.impl.MallCommoditySpecificationServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 商品规格表
 *
 * @author huangpengfei
 * @since 2022-02-11 16:55:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/commodity-specification")
@Tag(description = "commodity-specification", name = "商品规格表")
public class MallCommoditySpecificationController {
    private final MallCommoditySpecificationServiceImpl commoditySpecificationService;

    /**
     * 分页查询-同步获取商品与商品详情
     *
     * @param commoditySpecification 商品规格
     * @return 分页结果
     */
    @Operation(description = "分页查询-同步创建商品与商品详情", summary = "分页查询-同步创建商品与商品详情")
    @GetMapping("/async-commodity")
    public R<IPage<MallCommoditySpecificationVO>> getMallCommoditySpecificationPage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "pageSize", required = false, defaultValue = "1000") Integer pageSize,
            MallCommoditySpecification commoditySpecification) {
        return R.ok(commoditySpecificationService.getPage(Page.of(pageNumber, pageSize), commoditySpecification));
    }

    /**
     * 通过guid查询商品规格-同步获取商品与商品详情
     *
     * @param guid guid
     * @return R 返回结果
     */
    @Operation(description = "通过guid查询商品规格-同步获取商品与商品详情", summary = "通过guid查询商品规格-同步获取商品与商品详情")
    @GetMapping("/async-commodity/{guid}")
    public R<MallCommoditySpecificationVO> getByGuid(@PathVariable("guid") String guid) {
        return R.ok(commoditySpecificationService.getVOByGuid(guid));
    }

    /**
     * 新增商品规格
     *
     * @param mallCommoditySpecification 商品规格
     * @return R 返回结果
     */
    @Hidden
    @Operation(description = "新增商品规格", summary = "新增商品规格")
    //@SysLog("新增商品规格")
    @PostMapping
    public R<Boolean> save(@RequestBody MallCommoditySpecification mallCommoditySpecification) {
        return R.ok(commoditySpecificationService.save(mallCommoditySpecification));
    }

    /**
     * 商品发布-同步创建商品、商品规格、商品详情
     *
     * @param dto 商品规格
     * @return R 返回结果
     */
    @Operation(description = "新增商品规格-同步创建商品与商品详情", summary = "商品发布")
    //@SysLog("新增商品规格")
    @PostMapping("/async-commodity")
    public R<Boolean> saveAndCreateCommodity(@RequestBody @Valid MallCommoditySpecificationDTO dto) {
        return R.ok(commoditySpecificationService.saveAndCreateCommodity(dto));
    }

    /**
     * 修改商品规格
     *
     * @param mallCommoditySpecification 商品规格
     * @return R 返回结果
     */
    @Operation(description = "修改商品规格", summary = "修改商品规格")
    //@SysLog("修改商品规格")
    @PutMapping
    public R<Boolean> updateById(@RequestBody MallCommoditySpecification mallCommoditySpecification) {
        return R.ok(commoditySpecificationService.updateByGuid(mallCommoditySpecification));
    }

    /**
     * 通过guid删除商品规格
     *
     * @param guid guid
     * @return R 返回结果
     */
    //@SysLog("通过id删除商品")
    @Operation(description = "通过guid删除商品规格", summary = "通过guid删除商品规格")
    @DeleteMapping("/{guid}")
    public R<Boolean> removeById(@PathVariable String guid) {
        return R.ok(commoditySpecificationService.removeByGuid(guid));
    }
}
